#

| Chapter 12 |

# 12. Job Control

Contents:
Job Control: Work Faster, Stop Runaway Jobs
Other Ways to Refer to Jobs
The "Current Job" Isn't Always What You Expect
Job Control and autowrite: Real Time Savers!
System Overloaded? Try Stopping Some Jobs
Notification When Jobs Change State
Stop Background Output with stty tostop
Job Control in a Nutshell
Running Multiple Shell Sessions with screen

## 12.1 Job Control: Work Faster, Stop Runaway Jobs

Multitasking, letting you run more than one program at a time, is one of the great things about UNIX. Before job control, though, you had to decide ahead of time whether you wanted to run a job in the foreground (on your screen) or in the background (where you couldn't touch the program except to terminate it before it finished).

The C shell - and other shells since it, including some new Bourne shells - have job control built into them. You can start and stop jobs, pop them in and out of the background, and more. Windowing systems, which let you have multiple terminal windows active on the screen at the same time, make this less essential. Still, there are some important areas where you'll get more productivity out of job control than from simply opening another window. This article is an introduction to job control - there's more to learn.

Job control takes more than a shell to work right: the UNIX kernel has to support it. Berkeley UNIX since BSD 4.0 has had job control, so most Berkeley-type UNIXes will have it, too. Most versions of UNIX System V before Release 4 did not have job control. If your UNIX doesn't support job control, you can still put a job in the background - see the last paragraph in this article.

### 12.1.1 Foreground and Background

UNIX distinguishes between foreground and background programs. This feature allows you to run several programs simultaneously from your terminal. When a program is running in the foreground, anything you type at the keyboard is sent to the program's standard input unless you have redirected it. As a result, you can't do anything else until the program finishes. When you run a program in the background, it is disconnected from the keyboard. Anything you type reaches the UNIX shell and is interpreted as a command. Therefore, you can run many programs simultaneously in the background. You can run only one program at a time in the foreground.

To run a program in the background, type an ampersand (`&`) at the end of the command line. For example:

>     % f77 program.F &
>     [1] 9145
>     %

This runs a FORTRAN compilation in the background, letting you continue other work while the compilation proceeds. The shell responds by printing a job number in brackets (`[]`), followed by the process identification (PID) number (38.3) for the command. It then prompts you for a new command. Entering the command jobs produces a short report describing all the programs you are executing in the background. For example:

>     % f77 program.F &
>     [1] 9145
>     % jobs
>     [1]     + Running       f77 program.F
>     %

To bring a program from the background into the foreground, use the foreground command, fg. If you have more than one background job, follow fg with a job identifier - a percent sign (`%`) followed by the job number:

>     % jobs
>     [1]     - Running       f77 program.F
>     [2]     + Stopped       vi sinus.F
>     % fg %1

The plus sign (`+`) in the report from jobs indicates which job will return to the foreground by default (12.3).

To suspend a job running in the foreground, press CTRL-z. [You can use this to stop most frozen or runaway programs until you figure out what to do next. Also, CTRL-z can stop programs that interrupt characters (5.9) like CTRL-c can't. -JP ]

Entering the background command, bg, lets a stopped program continue execution in the background. The foreground command, fg, restores this program to execution in the foreground. For example:

>     % f77 -o program program.F
>     [CTRL-z]
>     Stopped
>     % bg
>     [1]     + Running       f77 -o program program.F
>     %

There is no prompt after the f77 command because the compiler is running in the foreground. After you press CTRL-z, the shell prints the word "Stopped" to indicate that it has stopped execution. At this point, you can enter any command; the bg command lets the job continue executing in the background. This feature is useful if you forget to type an `&` at the end of the command line or if you decide to do something else while the job is running.

To terminate a background job, you can use the command's job number rather than its PID number, as follows:

>     % kill %1

If you omit it, UNIX interprets the job number as a process number. This will probably be the process number of some operating system function. UNIX will not let you make such a mistake unless you are superuser (1.24). If you are superuser, the command is fatal. You may be superuser from time to time and therefore should not develop sloppy habits.

In the next few seconds, press RETURN a few times. You should see the message:

>     [1]  Terminated    f77 -o program program.F

If you don't see that, use the jobs command to check whether the job is still running. If it's still running, use the -9 option as a last resort:

>     % kill -9 %1
>     [1]  Killed        f77 -o program program.F

The -9 option doesn't give the process a chance to clean up its temporary files and exit gracefully, so don't use it unless you need to.

A program running in the background cannot read input from a terminal. If a background job needs terminal input, it will stop; the jobs command will print the message `Stopped (tty input)`. Before the program can continue, you must bring it into the foreground with the fg command and type the required input. You can save yourself this trouble by redirecting the program's input so that it reads all its data from a file. You may also want to redirect standard output and standard error. If you don't, any output the program produces will appear on your terminal (unless you've used stty tostop (12.7)). Since you will probably be using other commands, having miscellaneous data and other messages flying across your terminal may be confusing.

On systems and shells without job control features, an `&` will start a command in the background. It is impossible to move a job from the foreground to the background or vice versa. The ps (38.5) command is the only tool available for determining what background jobs you have running.

- ML from O'Reilly & Associates' UNIX for FORTRAN Programmers, Chapter 1

11.16 Instead of Changing History Characters |  | 12.2 Other Ways to Refer to Jobs